package cac.fenjian.resource.outStoreroom.rest;

import cac.fenjian.resource.finProReceiveSearch.rest.FinProReceiveSearchRest;
import cac.fenjian.resource.mssslshelves.service.MsSslShelvesService;
import cac.fenjian.resource.outStoreroom.sevice.OutStoreMaterialRoomService;
import cac.fenjian.resource.partreceivesearch.rest.PartReceiveRest;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/receiveCallWorkbin")
@Slf4j
public class OutStoreMaterialRoomRest {
    @Autowired
    private TransactionDefinition transactionDefinition;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private PartReceiveRest partReceiveRest;
    @Autowired
    private FinProReceiveSearchRest finProReceiveSearchRest;
    @Autowired
    private OutStoreMaterialRoomService outStoreMaterialRoomService;
    @Autowired
    private MsSslShelvesService msSslShelvesService;
    //4-11 v2.0
    /*零件——物料接收叫箱*/
    @PostMapping("partWorkbinOutStoreroom")
    public ResponseMsg<String>  partWorkbinOutStoreroom(@RequestBody HashMap map){
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String IP=null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        String receiveEmpCode = (String) request.getSession().getAttribute("account");
        if(Objects.isNull(receiveEmpCode)){
            responseMsg.setRetCode("201");
            responseMsg.setMessage("登录过期，请重新登录！");
            return responseMsg;
        }
        //事务管理
        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
        try {
            List<HashMap> lists = (List<HashMap>) map.get("data");
            for(HashMap data :lists){
                //改变接收状态
                partReceiveRest.changeTransferType(data);
            }
            //调用零件缓存箱出库
            responseMsg = outStoreMaterialRoomService.callBox(lists,IP);
            if(responseMsg.getRetCode().equals("201")){
                platformTransactionManager.rollback(transaction);
                return responseMsg;
            }
            //全部成功提交事务
            platformTransactionManager.commit(transaction);
            return responseMsg;
        }catch (NullPointerException ne){
            ne.printStackTrace();
            platformTransactionManager.rollback(transaction);
            return responseMsg.setRetCode("201").setMessage("叫箱失败！请确认是否还有空箱！");
        }catch (Exception e){
            e.printStackTrace();
            platformTransactionManager.rollback(transaction);
            return responseMsg.setRetCode("201").setMessage("接收失败");
        }
    }

    /*成品——物料接收叫箱*/
    @PostMapping("finWorkbinOutStoreroom")
    public ResponseMsg<String>  finWorkbinOutStoreroom(@RequestBody HashMap map){
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        String IP=null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        String receiveEmpCode = (String) request.getSession().getAttribute("account");
        if(Objects.isNull(receiveEmpCode)){
            responseMsg.setRetCode("201");
            responseMsg.setMessage("登录过期，请重新登录！");
            return responseMsg;
        }
        //事务管理
        TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
        try {
            List<HashMap> lists = (List<HashMap>) map.get("data");
            for(HashMap data :lists){
                //改变接收状态
                finProReceiveSearchRest.changeTransferType(data);
            }
            //调用成品缓存箱出库
            responseMsg = outStoreMaterialRoomService.callBox(lists,IP);
            if(responseMsg.getRetCode().equals("201")){
                platformTransactionManager.rollback(transaction);
                return responseMsg;
            }
            //全部成功提交事务
            platformTransactionManager.commit(transaction);
            return responseMsg;
        }catch (NullPointerException ne){
            ne.printStackTrace();
            platformTransactionManager.rollback(transaction);
            return responseMsg.setRetCode("201").setMessage("叫箱失败！请确认是否还有空箱！");
        }catch (Exception e){
            e.printStackTrace();
            platformTransactionManager.rollback(transaction);
            return responseMsg.setRetCode("201").setMessage("接收失败");
        }
    }

}
